package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* WalletView
* */
class WalletView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_2)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            strokeWidth = mStrokeWidth
            color = context.resources.getColor(R.color.color_9426A6)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_9426A6)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)
            mPath0.apply {
                reset()
                moveTo(-cx * 6 / 7, -cy * 6 / 7)
                lineTo(cx * 6 / 7, -cy * 6 / 7)
                lineTo(cx * 6 / 7, cy * 6 / 7)
                lineTo(-cx * 6 / 7, cy * 6 / 7)
                close()
            }
            drawPath(mPath0, mPaint0)

            mPath0.apply {
                reset()
                moveTo(cx * 6 / 7, cy * 3 / 7)
                lineTo(cx * 3 / 7, cy * 3 / 7)
                //boolean forceMoveTo  是否以圆弧为起点填充颜色
                //arcTo()要以顺时针方向绘制内容 否则会导致连接出现问题  填充颜色出现问题
                arcTo(
                    0F,
                    -cy * 3 / 7,
                    cx * 6 / 7,
                    cy * 3 / 7,
                    90F,
                    180F,
                    false
                )
                lineTo(cx * 6 / 7, -cy * 3 / 7)
            }
            drawPath(mPath0, mPaint0)

            drawCircle(
                cx * 3 / 7,
                   0F,
                cx / 7,
                mPaint1
            )
        }
    }
}